﻿
using System.IO.IsolatedStorage;

namespace PmuRegistry.Silverlight.Utilities
{
    public static class IsolatedStorageManager
    {
        /// <summary>
        /// Save property and value into isolated storage
        /// </summary>
        /// <param name="value">value to store</param>
        /// <param name="name">name of property</param>
        public static void SaveIntoIsolatedStorage(string name, object value)
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
            //If settings have never been saved, then we have to add appsetting first
            if (!appSettings.Contains(name))
                appSettings.Add(name, null);

            appSettings[name] = value;
            appSettings.Save();
        }

        public static bool Contains(string name)
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
            return appSettings.Contains(name);
        }

        /// <summary>
        /// Remove property value from isolated storage
        /// </summary>
        /// <param name="name">name of property</param>
        public static void Remove(string name)
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;

            //If settings have been saved, then we remove 
            if (appSettings.Contains(name))
                appSettings.Remove(name);
        }

        /// <summary>
        /// Removes all properties stored in isolated storage
        /// </summary>
        public static void RemoveAll()
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
            appSettings.Clear();
        }

        /// <summary>
        /// Retrieve setting that was stored in Isolated Storage
        /// </summary>
        /// <param name="name">name of the property to retrieve</param>
        /// <returns></returns>
        public static object LoadFromIsolatedStorage(string name)
        {
            IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
            if (appSettings.Contains(name))
            {
                return appSettings[name];
            }
            return null;
        }
    }
}
